package com.thb.pay.mchant.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.thb.pay.api.merchant.MchAppServiceApi;
import com.thb.pay.api.merchant.vo.req.MchAppPageReq;
import com.thb.pay.api.merchant.vo.req.MchAppReq;
import com.thb.pay.api.merchant.vo.resp.MchAppVo;
import com.thb.pay.core.exception.RRException;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.db.entity.SuperEntity;
import com.thb.pay.db.utils.PageResultUtils;
import com.thb.pay.db.utils.PageUtils;
import com.thb.pay.db.utils.QueryUtils;
import com.thb.pay.mchant.entity.MchApp;
import com.thb.pay.mchant.service.MchAppService;
import com.xiaoleilu.hutool.bean.BeanUtil;
import com.xiaoleilu.hutool.collection.CollUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 商户-应用表 前端控制器
 * </p>
 *
 * @author lucky
 * @since 2019-07-21
 */
@RestController
public class MchAppController implements MchAppServiceApi {

    @Autowired
    private MchAppService mchAppService;

    @Override
    public PageResult<MchAppVo> findPage(@RequestBody MchAppPageReq pageReq) {
        IPage<MchApp> currentPage = PageUtils.buildPage(pageReq);
        QueryWrapper wrapper = QueryUtils.buildQueryPage(pageReq);
        IPage<MchApp> pageResult = mchAppService.page(currentPage, wrapper);
        return PageResultUtils.build(pageResult, MchAppVo.class);
    }

    @Override
    public List<MchAppVo> findAllList() {
        List<MchAppVo> resultDataList = Lists.newArrayList();
        List<MchApp> targetDbResultList = mchAppService.selectList(null);
        targetDbResultList.forEach(dbResult -> {
            MchAppVo resultData = new MchAppVo();
            BeanUtil.copyProperties(dbResult, resultData);
            resultDataList.add(resultData);
        });
        return resultDataList;
    }

    @Override
    public List<MchAppVo> findListByIds(@RequestBody List<Long> ids) {
        List<MchAppVo> resultDataList = Lists.newArrayList();
        List<MchApp> resDataList = mchAppService.selectBatchIds(ids);
        resDataList.forEach(payProduct -> {
            MchAppVo resultData = new MchAppVo();
            BeanUtil.copyProperties(payProduct, resultData);
            resultDataList.add(resultData);
        });
        return resultDataList;
    }

    @Override
    public MchAppVo getById(@RequestParam("id") Long id) {
        MchAppVo resultData = null;
        MchApp dbResult = mchAppService.selectById(id);
        if (dbResult != null) {
            resultData = new MchAppVo();
            BeanUtil.copyProperties(dbResult, resultData);
        }
        return resultData;
    }

    @Override
    public void add(@RequestBody MchAppReq mchAppReq) {
        MchApp targetMchApp = new MchApp();
        BeanUtil.copyProperties(mchAppReq, targetMchApp);
        mchAppService.insert(targetMchApp);
    }

    @Override
    public void update(@RequestBody MchAppReq mchAppReq) {
        MchApp mchApp = mchAppService.getById(mchAppReq.getId());
        if (mchApp == null) {
            throw RRException.build("merchant.app.not.exist");
        }
        MchApp targetMchApp = new MchApp();
        BeanUtil.copyProperties(mchAppReq, targetMchApp);
        mchAppService.updateById(targetMchApp);
    }

    @Override
    public void deleteBatchIds(@RequestBody List<Long> ids) {
        LambdaQueryWrapper<MchApp> wrapper = new LambdaQueryWrapper();
        wrapper.in(MchApp::getId, ids);
        List<MchApp> targetDbResultList = mchAppService.selectList(wrapper);
        if (CollUtil.isNotEmpty(targetDbResultList)) {
            targetDbResultList.forEach(payType -> {
                payType.setDelFlag(SuperEntity.DEFAULT_DELETE_FLAG);
            });
            mchAppService.updateBatchById(targetDbResultList);
        }
    }
}
